package com.orangelife.webservice.service.propFeeComm.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response;

import org.jasypt.contrib.org.apache.commons.codec_1_3.binary.Base64;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeComm.IPropFeeCommService;
import com.orangelife.service.propFeeComm.model.PropFeeCommDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.utils.HttpUtils;
import com.orangelife.utils.PropertyUtils;
import com.orangelife.utils.StringUtils;
import com.orangelife.webservice.base.BaseService;
import com.orangelife.webservice.base.PageUtils;
import com.orangelife.webservice.service.propFeeComm.IPropFeeCommWebService;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation 2015. All Rights Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * propFeeComm类 的WebService基本作业
 * 
 * @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-12-02 14:35:52
 * ==============================================================
 */
@Service("webservice_propFeeComm")
public class PropFeeCommWebServiceImpl extends BaseService implements
		IPropFeeCommWebService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource(name = "propFeeCommService")
	private IPropFeeCommService propFeeCommService;

	@Override
	public Response addPropFeeComm(HttpServletRequest servletRequest,
			PropFeeCommDto propFeeComm) throws Exception {

		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		if (!StringUtils.isBlank(propFeeComm.getQrcodeInfo())) {
			InputStream inputStream = HttpUtils.doGetByConnection(propFeeComm
					.getQrcodeInfo());
			propFeeComm.setQrcode(input2byte(inputStream));
		}
		int result = propFeeCommService.addPropFeeComm(propFeeComm);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}

		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "添加实体成功");

		return this.normalResp();
	}

	public static final String input2byte(InputStream inStream)
			throws IOException {
		byte[] data = null;
		// 读取图片字节数组
		try {
			data = new byte[inStream.available()];
			inStream.read(data);
			inStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return new String(Base64.encodeBase64(data));
	}

	@Override
	public Response editPropFeeComm(HttpServletRequest servletRequest,
			PropFeeCommDto propFeeComm) throws Exception {
		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/
		Response error = this.validateUUId(propFeeComm.getCommID(), "commID");
		if (error != null) {
			return error;
		}

		/************************************************
		 * 输入参数检查
		 ************************************************/

		if (!StringUtils.isBlank(propFeeComm.getQrcodeInfo())) {
			InputStream inputStream = HttpUtils.doGetByConnection(propFeeComm
					.getQrcodeInfo());
			propFeeComm.setQrcode(input2byte(inputStream));
		}
		int result = propFeeCommService.editPropFeeComm(propFeeComm);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "修改实体成功");

		return this.normalResp();
	}

	@Override
	public Response deletePropFeeComm(HttpServletRequest servletRequest,
			String commID) throws Exception {
		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/
		Response error = this.validateUUId(commID, "commID");
		if (error != null) {
			return error;
		}
		int result = propFeeCommService.deletePropFeeCommByLogic(commID);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "删除实体成功");

		return this.normalResp();
	}

	@Override
	public Response findPropFeeCommByID(HttpServletRequest servletRequest,
			String commID) throws Exception {

		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/
		Response error = this.validateUUId(commID, "commID");
		if (error != null) {
			return error;
		}
		PropFeeCommDto propFeeComm = propFeeCommService
				.findPropFeeCommByID(commID);

		Map<String, Object> maps = new HashMap<String, Object>();

		if (propFeeComm != null) {

		}

		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "查询实体成功");
		return this.normalResp(propFeeComm);
	}

	@Override
	public Response getAllPropFeeComm(HttpServletRequest servletRequest,
			String search, String tsCreate, String pageIndex, String pageSize)
			throws Exception {

		PageUtils pageUtils = this.validatePageAndSetPage(servletRequest,
				pageIndex, pageSize);
		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		/************************************************
		 * 查询列表
		 ************************************************/
		List<Map<String, Object>> list = propFeeCommService
				.findPropFeeCommByPage(tsCreate == null ? null : tsCreate,
						search == null ? null : search,
						pageUtils.getiPageIndex(), pageUtils.getiPageSize());

		/************************************************
		 * 返回结果设定
		 ************************************************/
		Long total = ((Page<Map<String, Object>>) list).getTotal();

		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginName() + new Date()
				+ "查询实体成功");

		return this.normalResp(pageUtils.getiPageIndex(),
				pageUtils.getiPageSize(), total,
				PropertyUtils.turnToEntity(list));
	}

	@Override
	public Response bauthFindPropFeeComm(HttpServletRequest servletRequest,
			List<String> propFeeComm) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (propFeeComm == null || propFeeComm.size() <= 0) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}

			map.put("pageEntity",
					propFeeCommService.bauthfindPropFeeComm(propFeeComm));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response bauthFindPropFeeComms(HttpServletRequest servletRequest,
			Map<String, Object> propFeeComm) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (propFeeComm == null || propFeeComm.size() <= 0) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}

			map.put("pageEntity", PropertyUtils.turnToEntity(propFeeCommService
					.bauthfindPropFeeCommByMap(propFeeComm)));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response clearCache(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		propFeeCommService.clearCache();
		return this.normalResp();
	}

}
